package com.doyens.gmall.all.controller;

import com.doyens.gmall.common.result.Result;
import com.doyens.gmall.list.client.ListFeignClient;
import com.doyens.gmall.model.list.SearchAttr;
import com.doyens.gmall.model.list.SearchParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.*;
import java.util.stream.Collectors;

@Controller
public class ListController {
    @Autowired
    private ListFeignClient listFeignClient;

    @GetMapping("list.html")
    public String search(SearchParam searchParam, Model model) {
        Result<Map> list = listFeignClient.list(searchParam);
        //addAllAttributes 放一个map到域中 map(pageNo,1)
        model.addAllAttributes(list.getData());
        //页面需要携带排序数据 order是个字符串，1:desc  2:asc  1:asc 2:desc  null ""   map<type/sort >
        //1:hotScore 2:price
        Map orderMap = dealOrder(searchParam.getOrder());
        model.addAttribute("orderMap", orderMap);
        //searchParam ：搜索的条件，传回到页面，品牌的面包屑有关系,searchParam中的品牌不为空，从域中取trademarkParam显示数据
        model.addAttribute("searchParam", searchParam);
        //为了能显示品牌面包屑，还得手动放trademarkParam到域中,内容格式 品牌:华为,品牌在searchParam对象的trademark属性中，格式为 2:华为
        String trademarkParam = makeTrademark(searchParam);
        model.addAttribute("trademarkParam", trademarkParam);

        //为了能显示平台属性面包屑，需要有个list<对象>,对象有attrId，attrName，attrVlue属性，把list放入域中
        List<SearchAttr> propsParamList = makeProps(searchParam);
        model.addAttribute("propsParamList",propsParamList);

        //在地址栏显示搜索条件，方便在当前搜索基础上继续搜索
        String urlParam = makeUrlParam(searchParam);
        System.out.println("urlParam:" + urlParam);
        model.addAttribute("urlParam", urlParam);
        return "list/index";
    }

    private List<SearchAttr> makeProps(SearchParam searchParam) {
        //props 页面上的平台属性字符串数组，每个字符串的格式 23:4G:运行内存
        String[] props = searchParam.getProps();
        List<SearchAttr> propsParamList;
        if (props != null) {
            propsParamList = Arrays.stream(props).map(prop -> {
                String[] split = prop.split(":");
                if (split != null && split.length == 3) {
                    String attrId = split[0];
                    String attrValue = split[1];
                    String attrName = split[2];
                    SearchAttr searchAttr = new SearchAttr();
                    searchAttr.setAttrId(Long.parseLong(attrId));
                    searchAttr.setAttrName(attrName);
                    searchAttr.setAttrValue(attrValue);
                    return searchAttr;
                }
                return null;
            }).collect(Collectors.toList());
            return propsParamList;
        }else{
            propsParamList = new ArrayList<>();
        }
        return  propsParamList;
    }

    private String makeTrademark(SearchParam searchParam) {
        String trademark = searchParam.getTrademark();
        if (StringUtils.isNotBlank(trademark)) {
            //trademark的格式 2:华为
            String[] split = trademark.split(":");
            if (split != null && split.length == 2) {
                return "品牌：" + split[1];
            }
        }
        return "";
    }

    private String makeUrlParam(SearchParam searchParam) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("list.html?");
        if (searchParam.getCategory1Id() != null) {
            stringBuilder.append("category1Id=" + searchParam.getCategory1Id());
        }
        if (searchParam.getCategory2Id() != null) {
            stringBuilder.append("category2Id=" + searchParam.getCategory2Id());
        }
        if (searchParam.getCategory3Id() != null) {
            stringBuilder.append("category3Id=" + searchParam.getCategory3Id());
        }
        if (searchParam.getKeyword() != null) {
            stringBuilder.append("keyword=" + searchParam.getKeyword());
        }
        //拼接品牌
        if (searchParam.getTrademark() != null) {
            //拼接品牌
            stringBuilder.append("&trademark=" + searchParam.getTrademark());
        }
        //拼接平台属性
        String[] props = searchParam.getProps();
        if (props != null && props.length > 0) {
            for (String prop : props) {
                //prop id:值:名字
                stringBuilder.append("&props=" + prop);
            }
        }

        return stringBuilder.toString();

    }

    /**
     * order是个字符串，1:desc  2:asc  1:asc 2:desc  null ""   map<type/sort >
     * 1:hotScore 2:price
     *
     * @param order
     * @return map集合，需要key type sort
     */
    public Map dealOrder(String order) {
        Map<String, String> map = new HashMap<>();
        if (!StringUtils.isBlank(order)) {
            //解析
            String[] split = order.split(":");
            if (split != null && split.length == 2) {
                map.put("type", split[0]);
                map.put("sort", split[1]);
                return map;
            }
        }
        map.put("type", "1");
        map.put("sort", "hotScore");
        return map;

    }
}
